import pandas as pd
import numpy as np
from tensorflow import keras
from sklearn.preprocessing import LabelEncoder,MinMaxScaler

data = pd.read_csv('footdata.csv')

features = ['avgPrice','type','tasterate','envsrate','serverate']
target = 'start'

label_encoder = LabelEncoder()
data['type'] = label_encoder.fit_transform(data['type'])

scaler_x = MinMaxScaler()
scaler_y = MinMaxScaler()

x = scaler_x.fit_transform(data[features])
y = scaler_y.fit_transform(data[[target]])

time_steps = 5
x_sep = []
y_sep = []

for i in range(time_steps,len(x)):
    x_sep.append(x[i-time_steps:i])
    y_sep.append(y[i])

x_sep,y_sep = np.array(x_sep),np.array(y_sep)

model = keras.Sequential([
    keras.layers.LSTM(50,input_shape=(x_sep.shape[1],x_sep.shape[2]),return_sequences=True),
    keras.layers.LSTM(50),
    keras.layers.Dense(1)
])

model.compile(optimizer='adam',loss='mean_squared_error')
model.fit(x_sep,y_sep,epochs = 50,batch_size=32)

def predict_start(single_data):
    single_data_df = pd.DataFrame([single_data])
    single_data_df['type']= label_encoder.transform([single_data['type']])
    single_data_scaled = scaler_x.transform(single_data_df)
    new_data_sep =[]
    for i in range(time_steps):
        new_data_sep.append(single_data_scaled[0])
    new_data_sep = np.array(new_data_sep)
    new_data_sep = np.reshape(new_data_sep,(1, time_steps, single_data_scaled.shape[1]))

    y_pred_scaled = model.predict(new_data_sep)
    y_pred = scaler_y.inverse_transform(y_pred_scaled)
    return y_pred[0,0]

singleDataExample = {
  'avgPrice': 20,
  'type': '小吃快餐',
  'tasterate': 8,
  'envsrate': 7.6,
  'serverate': 7.6
};

predicted_value = predict_start(singleDataExample);
print(predicted_value)
